<template>
  <div class="box">
    <el-row>
      <h3 style="margin-top: 0px">{{ $route.query.title }}</h3>
    </el-row>
    <el-form ref="form" :model="form" label-width="100px">
      <el-form-item label="新闻标题：">
        <el-input v-model="form.topic"></el-input>
      </el-form-item>
      <el-form-item label="新闻作者：">
        <el-input v-model="form.author"></el-input>
      </el-form-item>
      <el-form-item label="投稿时间：">
        <el-col :span="11">
          <el-date-picker v-model="form.subTime" type="date" placeholder="选择日期">
          </el-date-picker>
        </el-col>
      </el-form-item>
      <el-form-item label="点击量：">
        <el-input v-model="form.clickNum"></el-input>
      </el-form-item>
      <el-form-item label="部门：">
        <el-select v-model="form.departmentId" placeholder="请选择部门">
          <el-option label="区域一" value="1"></el-option>
          <el-option label="区域二" value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="专题：">
        <el-select v-model="form.specialId" placeholder="请选择专题">
          <el-option label="区域一" value="1"></el-option>
          <el-option label="区域二" value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="栏目：">
        <el-radio-group v-model="form.columnId">
          <el-radio label="1">消防要闻</el-radio>
          <el-radio label="2">机关动态</el-radio>
          <el-radio label="3">领导讲话</el-radio>
          <el-radio label="4">视频点播</el-radio>
          <el-radio label="5">机关文件</el-radio>
          <el-radio label="6">基层动态</el-radio>
          <el-radio label="7">灭火救援</el-radio>
          <el-radio label="8">政治工作</el-radio>
          <el-radio label="9">后勤保障</el-radio>
          <el-radio label="10">火灾防控</el-radio>
          <el-radio label="11">纪委工作</el-radio>
          <el-radio label="12">工作简报</el-radio>
          <el-radio label="13">工作研讨</el-radio>
          <el-radio label="14">媒体聚焦</el-radio>
          <el-radio label="15">精品文摘</el-radio>
          <el-radio label="19">通知公告</el-radio>
          <el-radio label="20">本埠报道</el-radio>
          <el-radio label="21">警营文化</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="是否拒绝：">
        <el-radio-group v-model="form.isRefuse">
          <el-radio label="0">是</el-radio>
          <el-radio label="1">否</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="新闻内容：">
        <quill-editor ref="text" v-model="form.content" class="myQuillEditor" :options="editorOption" />
      </el-form-item>
      <el-form-item style="margin-top: 130px;">
        <el-button type="primary" @click="onSubmit">发布</el-button>
        <el-button type="primary" @click="View">预览</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { quillEditor, Quill } from 'vue-quill-editor'
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
import { ImageExtend, QuillWatch } from 'quill-image-extend-module'
Quill.register('modules/ImageExtend', ImageExtend)
// axios.defaults.withCredentials = true
import axios from 'axios'
export default {
  name: 'editPage',
  components: { quillEditor },
  data() {
    return {
      form: {
        author: '',
        departmentId: '',
        specialId: '',
        columnId: '',
        topic: '',
        content: '',
        clickNum: '',
        subTime: '',
        isRefuse: '1',
      },
      images:[],
      videos:[],
      picture: null,
      photo: '',
      newsCategoryId: 2,
      editorOption: {
        placeholder: '请输入正文',
        modules: {
          ImageExtend: {
            // 如果不作设置，即{}  则依然开启复制粘贴功能且以base64插入
            name: 'file', // 图片参数名
            loading: true,
            size: 10, // 可选参数 图片大小，单位为M，1M = 1024kb
            action: 'http://localhost:8090/files/upload',
            response: res => {
              console.log(res)
              return 'http://localhost:8090/' + res.data
            },
            headers: xhr => {
              console.log(xhr)
              // axios.defaults.withCredentials = true;
              // xhr.setRequestHeader('Cookie','NMTID=00OVM6QOJcTDVhqDUtvgSnd-5FBxLcAAAF9nTiDHg; JSESSIONID=5ACC52D29A2434E09450CB975E08C288')
            }, // 可选参数 设置请求头部
            sizeError: () => { }, // 图片超过大小的回调
            start: () => { }, // 可选参数 自定义开始上传触发事件
            end: () => { }, // 可选参数 自定义上传结束触发的事件，无论成功或者失败
            error: () => { }, // 可选参数 上传失败触发的事件
            success: () => { }, // 可选参数  上传成功触发的事件
            change: (xhr, formData) => {
              console.log(xhr, formData)
              // formData.append('token', 'myToken')
            } // 可选参数 每次选择图片触发，也可用来设置头部，但比headers多了一个参数，可设置formData
          },
          toolbar: {
            container: [
              ['bold', 'italic', 'underline', 'strike'], // 加粗，斜体，下划线，删除线
              ['blockquote', 'code-block'], // 引用，代码块
              [{ header: 1 }, { header: 2 }], // 标题，键值对的形式；1、2表示字体大小
              [{ list: 'ordered' }, { list: 'bullet' }], // 列表
              [{ script: 'sub' }, { script: 'super' }], // 上下标
              [{ indent: '-1' }, { indent: '+1' }], // 缩进
              [{ direction: 'rtl' }], // 文本方向
              [{ size: ['small', false, 'large', 'huge'] }], // 字体大小
              [{ header: [1, 2, 3, 4, 5, 6, false] }], // 几级标题
              [{ color: [] }, { background: [] }], // 字体颜色，字体背景颜色
              [{ font: [] }], // 字体
              [{ align: [] }], // 对齐方式
              ['clean'], // 清除字体样式
              ['image'] // 上传图片、上传视频
            ],
            handlers: {
              image: function () {
                // 劫持原来的图片点击按钮事件
                QuillWatch.emit(this.quill.id)
              }
            }
          }
        }
      }, // 编辑器新闻对象
      fileList: []
    }
  },
  created() {
    this.fetchArticleData();
  },
  methods: {
    fetchArticleData() {
      const articleId = this.$route.params.articleId;
      axios.get(`/article/findByArtId/${articleId}`)
        .then(response => {
          if (response.data.code === 200) {
            this.form = response.data.data;
            this.form.isRefuse = response.data.data.isRefuse.toString();
            this.form.columnId = response.data.data.columnId.toString();
            console.log('成功获取文章数据:', this.form);
          } else {
            console.error('获取文章数据时出错:', response.data.msg);
          }
        })
        .catch(error => {
          console.error('Error fetching article data:', error);
        });
    },
    onSubmit() {  
      const articleId = this.$route.params.articleId;
      console.log(articleId)
      axios.put(`/article/publish/${articleId}`) 
        .then(response => {  
          if (response.data.code === 200) {   
            alert('稿件发布成功！');  
          } else {   
            alert('稿件发布失败：' + response.data.message);  
          }  
        })  
        .catch(error => {  
          console.error(error);  
          alert('网络错误或服务器异常！');  
        });  
    },  
    View(){
      const articleId = this.$route.params.articleId;
      this.$router.push({ name: 'ViewArticle', params: { articleId } })
    }
  }
}
</script>

<style lang="less" scoped>
.box {
  width: 1200px;
  margin: 0 auto;
}

.myQuillEditor {
  height: 350px;
}

.btn {
  position: absolute;
  bottom: 20px;
  left: 168px;
}

.ql-editor img {
  width: 100px !important;
  height: 100px !important;
}
</style>
